<?php
namespace backend\controllers;

use Yii;
use yii\web\AdminController;
use backend\models\UploadForm;
use backend\models\GoodsIdentification;
use common\helps\tools;
use backend\models\ContentArticle;

class ArticleIdentificationController extends AdminController
{
    public $layout = false;
    //业务关键字
    public $service = 'ArticleIdentification';
    
    public $type = 3;
    
    public function actionIndex()
    {
        $model = new UploadForm();
        return $this->render('index',['model'=>$model]);
    }
    
    public function actionGetArticleIdentification(){
        $data = Yii::$app->request->post();
        $query = GoodsIdentification::find()->select('id,name,sort,use_flg')->where('type='.$this->type);
        $count = $query->count();
        $sort_string = empty($data['columns'][$data['order'][0]['column']]['data']) ? 'id' : $data['columns'][$data['order'][0]['column']]['data'];
        $list = $query->offset($data['start'])->limit($data['length'])->orderBy(
            [$sort_string=>$data['order'][0]['dir'] == 'asc' ? SORT_ASC : SORT_DESC])->asArray()->all();
        $result['draw'] = intval($data['draw']);
        $result['recordsTotal'] = intval($count);
        $result['recordsFiltered'] = intval($count);
        $result['data'] = $list;
        return \yii\helpers\Json::encode($result);
    }
    
    public function actionChangeArticleIdentificationStatus(){
        $model = new GoodsIdentification();
        $data = Yii::$app->request->post();
        $id = $data['id'];
        $use_flg = $data['use_flg'];
        if ($use_flg != '1'){
            $find_article = $this->checkIdentificationUsed($id);
            if ($find_article){
                $this->retJson('该标识已被文章使用，请确认',500);
            }
        }
        $data_new = [
            'use_flg'=>$use_flg,
            'editor'=>$model->getCreator(),
            'edit_time'=>date('Y-m-d H:i:s')
        ];
        $ret = $model::updateAll($data_new,['id'=>$id]);
        if(!$ret){
            $this->retJson('操作失败',500);
        }
        $this->retJson('操作成功',200);
    }
    
    public function checkIdentificationUsed($id){
        $find_article = ContentArticle::find()->where(['article_identification'=>$id])->count();
        if ($find_article > 0){
            return true;
        }
        return false;
    }
    
    public function actionChangeArticleIdentification(){
        $model = new GoodsIdentification();
        $data = Yii::$app->request->post();
        try {
            $article_identification_id = $data['article_identification_id'];
            $name = $data['name'];
            if (!$name) {
                throw new \Exception('请填写标识');
            }
            $sort = $data['sort'];
            if (!$sort) {
                throw new \Exception('请填写排序编号');
            }
            $use_flg = $data['use_flg'];
            $oper = $model->getCreator();
            $operTime = date('Y-m-d H:i:s',time());
            if ($article_identification_id){
                if ($use_flg != '1'){
                    $find_article = $this->checkIdentificationUsed($article_identification_id);
                    if ($find_article){
                        throw new \Exception('该标识已被文章使用，请确认');
                    }
                }
                $model = $model->findOne($article_identification_id);
                $find_name = GoodsIdentification::find()->where(['name'=>$name])->asArray()->one();
                if (tools::arrayHasValue($find_name,'id') && $find_name['id'] != $article_identification_id){
                    throw new \Exception('您提交的标识已存在，请确认');
                }
                $model->editor = $oper;
                $model->edit_time = $operTime;
            }else{
                $find_name = GoodsIdentification::find()->where(['name'=>$name])->asArray()->one();
                if (tools::arrayHasValue($find_name,'id')){
                    throw new \Exception('您提交的标识已存在，请确认');
                }
                $model->creator = $oper;
                $model->create_time = $operTime;
                $model->type = $this->type;
            }
            $model->name = $name;
            $model->sort = $sort;
            $model->use_flg = $use_flg;
            $model->save();
            $id = $model->attributes['id'];
            if (!$id){
                throw new \Exception('文章信息操作失败');
            }
            $this->retJson('操作成功',200);
        } catch (\Exception $e) {
            error_log('error->' . __METHOD__ . ' error:' . $e->getMessage());
            $this->retJson($e->getMessage(),500);
        }
    }
}